package vsvm.gui.forms;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import vsvm.classifier.AbstractTestSet;
import vsvm.exceptions.IncompatableTestSetsException;
import vsvm.gui.Environment;
import vsvm.math.statistics.McNemarTest;

import javax.swing.JTextPane;
import javax.swing.JButton;

import org.apache.commons.math.distribution.ChiSquaredDistributionImpl;
import org.apache.commons.math.distribution.NormalDistributionImpl;

public class McNemarTestDialog extends JDialog{

	private JPanel jContentPane = null;
	private JPanel jCentralPanel = null;
	private JPanel jButtonsPanel = null;
	private JButton jTestButton = null;
	private JButton jCancelButton = null;
	private JLabel jComboBoxLabel = null;
	private JComboBox jComboBox = null;	
	
	private AbstractTestSet currentTest = null;
	
	
	
	private class DialogCancelAction extends AbstractAction {
		private McNemarTestDialog dlg;
		
		public DialogCancelAction(McNemarTestDialog d) {
			dlg = d;			
		}

		public void actionPerformed(ActionEvent e) {
			dlg.dispose();			
		}
	}
	
	
	
	public McNemarTestDialog(AbstractTestSet testSet) {
		super();
		this.currentTest = testSet;
		initialize();
		
	}

	private void initialize() {
        this.setSize(new java.awt.Dimension(311,137));
        this.setResizable(false);
        this.setContentPane(getJContentPane());
        this.setTitle("McNamer's Test Settings");
        this.setLocationRelativeTo(Environment.getEnvironment().getMainFrame());			
	}

	/**
	 * This method initializes jContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJButtonsPanel(), java.awt.BorderLayout.SOUTH);
			jContentPane.add(getJCentralPanel(), java.awt.BorderLayout.CENTER);
			
		}
		return jContentPane;
	}

	/**
	 * This method initializes jCentralPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJCentralPanel() {
		if (jCentralPanel == null) {
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints1.gridy = 2;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.gridwidth = 1;
			gridBagConstraints1.insets = new java.awt.Insets(4,20,0,20);
			gridBagConstraints1.gridx = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 1;
			gridBagConstraints.insets = new java.awt.Insets(0,20,0,0);
			gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints.gridwidth = 1;
			gridBagConstraints.ipadx = 0;
			gridBagConstraints.gridy = 1;
			jComboBoxLabel = new JLabel();
			jComboBoxLabel.setText("Select test set for comparisson");
			jCentralPanel = new JPanel();
			jCentralPanel.setLayout(new GridBagLayout());
			jCentralPanel.add(jComboBoxLabel, gridBagConstraints);
			jCentralPanel.add(getJComboBox(), gridBagConstraints1);
		}
		return jCentralPanel;
	}

	/**
	 * This method initializes jButtonsPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJButtonsPanel() {
		if (jButtonsPanel == null) {
			jButtonsPanel = new JPanel();
			jButtonsPanel.add(getJTestButton(), null);
			jButtonsPanel.add(getJCancelButton(), null);
		}
		return jButtonsPanel;
	}

	/**
	 * This method initializes jTestButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJTestButton() {
		if (jTestButton == null) {
			jTestButton = new JButton();
			jTestButton.setText("Test");
			jTestButton.setPreferredSize(new java.awt.Dimension(80,25));
			jTestButton.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					doTest();
				}
			});
		}
		return jTestButton;
	}

	/**
	 * This method initializes jCancelButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJCancelButton() {
		if (jCancelButton == null) {
			jCancelButton = new JButton();
			jCancelButton.setAction(new DialogCancelAction(this));
			jCancelButton.setText("Cancel");
			jCancelButton.setPreferredSize(new java.awt.Dimension(80,25));			
		}
		return jCancelButton;
	}

	/**
	 * This method initializes jComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			jComboBox = new JComboBox();
			jComboBox.setModel(this.getCompatableTestSets(this.currentTest));
			if (jComboBox.getModel().getSize() == 0) {
				this.jTestButton.setEnabled(false);
			}
		}	
		
		return jComboBox;
	}
	
	//TODO more strict compatability test
	private DefaultComboBoxModel getCompatableTestSets(AbstractTestSet set) {
		
		DefaultComboBoxModel model = new DefaultComboBoxModel();
		ArrayList <AbstractTestSet> sets = Environment.getEnvironment().getWorkspace().getTestSets();		
		
		for (int i = 0; i < sets.size(); i++) {
			AbstractTestSet curSet = sets.get(i);
			if (!set.equals(curSet))
				model.addElement(sets.get(i));
		}
		
		return model;
		
	}
	
	private void doTest() {
		if (this.jComboBox.getSelectedItem() instanceof AbstractTestSet) {
			AbstractTestSet selectedTest = (AbstractTestSet)this.jComboBox.getSelectedItem();
			try {
				McNemarTest mt = new McNemarTest(this.currentTest, selectedTest);
				McNemarTestResultFrame mFrame = new McNemarTestResultFrame(mt);
				Environment.getEnvironment().getMainFrame().addWindow(mFrame);
				mFrame.setVisible(true);
			} catch (IncompatableTestSetsException e) {
				System.out.println(e.getDetails());
			}
		}
		
		this.dispose();
	}
	
	
}  //  @jve:decl-index=0:visual-constraint="40,6"
